package com.woniuxy.background.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.background.common.RestResult;
import com.woniuxy.background.pojo.Cts;
import com.woniuxy.background.pojo.Ts;
import com.woniuxy.background.service.CtsService;
import com.woniuxy.background.service.TsService;
import com.woniuxy.background.vo.TsVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/ts")
public class TsController {

    @Autowired
    private TsService tsService;

    @Autowired
    private CtsService ctsService;

    @GetMapping("/list")
    @ResponseBody
    @RequiresPermissions("ts:ts:list")
    public RestResult list(@RequestParam(value = "page",defaultValue = "1",required = false) Integer currentPage,
                           @RequestParam(value = "limit",defaultValue = "5",required = false) Integer pageSize,
                           TsVO vo){
        List<Ts> ts = tsService.queryTsByPage(currentPage,pageSize,vo);
        PageInfo pageInfo = new PageInfo(ts);
        return RestResult.success(pageInfo);
    }

    @GetMapping("/toCts/{tid}")
    public ModelAndView toCts( ModelAndView modelAndView,@PathVariable("tid")int tid){
        Cts cts = ctsService.queryCtsByTid(tid);
        if (cts != null){
            modelAndView.addObject("cts",cts);
        }
        modelAndView.addObject("tid",tid);
        modelAndView.setViewName("ts/editTs");
        return modelAndView;
    }

    @PostMapping("/saveCts")
    @ResponseBody
    @RequiresPermissions("ts:cts:save")
    public RestResult saveCts(Cts cts){
        String name = SecurityUtils.getSubject().getPrincipal().toString();
        cts.setcTime(new Date());
        cts.setcName(name);
        Ts ts = new Ts();
        ts.settStatus("已处理");
        ts.setTid(cts.getTid());
        try {
            ctsService.insertSelective(cts);
            tsService.updateByPrimaryKeySelective(ts);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failed("回复失败");
        }
        return RestResult.success("回复成功");
    }
    /*
    * ？？？？？？？？？？？
    * */
    @GetMapping("/dltCts/{cid}")
    @ResponseBody
    @RequiresPermissions("ts:cts:delete")
    public RestResult deleteCts(@PathVariable("cid") int cid){
        try {
            Cts c = ctsService.selectByPrimaryKey(cid);
            System.out.println(c);
            Ts ts = new Ts();
            ts.setTid(c.getTid());
            ts.settStatus("未处理");
            tsService.updateByPrimaryKeySelective(ts);
            ctsService.deleteByPrimaryKey(cid);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failed("删除失败");
        }
        return RestResult.success("删除成功");
    }
    @DeleteMapping("/deleteTs/{tid}")
    @ResponseBody
    @RequiresPermissions("ts:ts:delete")
    public RestResult deleteTs(@PathVariable("tid")int tid){
        try {
            Cts cts = ctsService.queryCtsByTid(tid);
            if (cts != null){
                ctsService.deleteByPrimaryKey(cts.getCid());
            }
            tsService.deleteByPrimaryKey(tid);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failed("删除失败");
        }
        return RestResult.success("删除成功");
    }
    @PostMapping("/updCts")
    @ResponseBody
    @RequiresPermissions("ts:cts:update")
    public RestResult updCts(Cts cts){
        System.out.println(cts);
        try {
            ctsService.updateByPrimaryKeySelective(cts);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failed("修改失败");
        }
        return RestResult.success("修改成功");
    }

}
